﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightGolf.Physics;
using System.Collections.Generic;
using SilverlightGolf.Core;


namespace SilverlightGolf.Game
{
    public class Hole
    {
        public event EventHandler<HoleStateEventArgs> StateChanged;
        public GolfGame Game { get; private set; }
        public ListWithEvents<IGameObject> GameObjects { get; private set; }
        public IList<PlayersBall> PlayersBalls { get; private set; }

        private HoleState _currentState;
        public HoleState CurrentState
        {
            get
            {
                return _currentState;
            }
            private set
            {
                _currentState = value;
                if (StateChanged != null)
                {
                    StateChanged(this, new HoleStateEventArgs(this,_currentState));
                }
            }
        }


        public SimulationEngine Engine { get; private set; }

        public Hole(GolfGame game)
        {
            Game = game;

            GameObjects = new ListWithEvents<IGameObject>();
            PlayersBalls = new List<PlayersBall>();
            Engine = new SimulationEngine();
            CurrentState = new NotReady();
        }

        public void Initialize()
        {
            foreach (var player in Game.Players)
            {
                var playersBall = new PlayersBall(player);
                
                GameObjects.Add(playersBall);
                Engine.Geoms.Add(playersBall.Geom);
                PlayersBalls.Add(playersBall);
            }

            CurrentState = new ReadyForShot() { PlayersBall = PlayersBalls[0] };
        }

        public void Update()
        {
            Engine.Update(0.017);
            if (Engine.AllStopped)
            {
                CurrentState = new ReadyForShot() { PlayersBall = PlayersBalls[0] };
            }
        }
    }
}
